from sqlalchemy import create_engine, Column, Integer, String, ForeignKey, Date, Float
from sqlalchemy.orm import relationship
from setting import Base, engine, Session

class Custom(Base):
	__tablename__ = 'customs'

	id = Column(Integer, primary_key=True)
	name = Column(String)

	def __init__(self, name):
		self.name = name

	def __unicode__(self):
		return self.name

	def __repr__(self):
		return self.name
	
class Grade(Base):
	__tablename__ = 'grades'

	id = Column(Integer, primary_key=True)
	name = Column(String)

	def __init__(self, name):
		self.name = name

	def __unicode__(self):
		return self.name

	def __repr__(self):
		return self.name
	
class ExtrusionOrder(Base):
	__tablename__ = 'extrusion_orders'

	id = Column(Integer, primary_key=True)
	custom_id = Column(Integer, ForeignKey('customs.id'))
	custom = relationship(Custom)
	date = Column(Date, nullable=False)
	weight = Column(Float)
	grade_id = Column(Integer, ForeignKey('grades.id'))
	grade = relationship(Grade)
	shape = Column(String)
	rough_spec = Column(String)
	status = Column(String)
	# out side diatolerance
	odt = Column(String)
	# in side diatolerance
	idt = Column(String)
	# length tolerance
	lt = Column(String)
	# wallthickness tolerance
	wt = Column(String)
	product_spec = Column(String)
	ingot_length = Column(String)
	extrusion_die = Column(String)
	count = Column(Integer)
	process_setting = Column(String)
	memo = Column(String)

	def __init__(self, custom, date, grade, weight=0, shape=None,
			rough_spec = None, status = None, odt = None, idt = None,
			lt = None, wt = None, product_spec = None, ingot_length = None,
			extrusion_die = None, count = 0, process_setting = None,
			memo = None):
		self.custom = custom
		self.date = date
		self.grade = grade
		self.weight = weight
		self.shape = shape
		self.rough_spec = rough_spec
		self.status = status
		self.odt = odt
		self.idt = idt
		self.lt = lt
		self.wt = wt
		self.product_spec = product_spec
		self.ingot_length = ingot_length
		self.extrusion_die = extrusion_die
		self.count = count
		self.process_setting = process_setting
		self.memo = memo

class StretchOrder(Base):
	__tablename__ = 'stretch_orders'

	id = Column(Integer, primary_key=True)
	custom_id = Column(Integer, ForeignKey('customs.id'))
	date = Column(Date, nullable=False)
	weight = Column(Float)

def test_data():
	import datetime

	session = Session()
	for i in xrange(10):
		c = Custom("custom %d" % i)
		session.add(c)
		g = Grade("Grade %d" % i)
		session.add(g)
		e = ExtrusionOrder(c, datetime.date(2010, 8, 10), g)
		session.add(e)
	session.commit()
	

if __name__ == '__main__':
	metadata = Base.metadata
	metadata.drop_all(engine)
	metadata.create_all(engine)
	test_data()
